﻿<UserControl x:Class="LaunchMeNot.StartupEntryDataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
    xmlns:dgp="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
    xmlns:lex="http://schemas.root-project.org/xaml/presentation"
    xmlns:conv="clr-namespace:LaunchMeNot.Converters" 
    xmlns:loc="clr-namespace:LaunchMeNot">
    <UserControl.CommandBindings>
        <CommandBinding Command="NavigationCommands.Refresh" Executed="RefreshEntries"/>
    </UserControl.CommandBindings>
    <UserControl.Resources>
        <Image x:Key="convert_16" x:Shared="False" Source="pack://application:,,,/Resources/images/convert_16.png"/>
        <Image x:Key="copy_16" x:Shared="False" Source="pack://application:,,,/Resources/images/copy_16.png"/>
        <Image x:Key="delete_16" x:Shared="False" Source="pack://application:,,,/Resources/images/delete_16.png"/>
        <Image x:Key="directory_16" x:Shared="False" Source="pack://application:,,,/Resources/images/directory_16.png"/>
        <Image x:Key="edit_16" x:Shared="False" Source="pack://application:,,,/Resources/images/edit_16.png"/>
        <Image x:Key="launch_16" x:Shared="False" Source="pack://application:,,,/Resources/images/icon_16.png"/>
        <Image x:Key="registry_16" x:Shared="False" Source="pack://application:,,,/Resources/images/registry_16.png"/>
        <Image x:Key="startup_16" x:Shared="False" Source="pack://application:,,,/Resources/images/startup_16.png"/>
        <Image x:Key="search_16" x:Shared="False" Source="pack://application:,,,/Resources/images/search_16.png"/>
        
        <conv:EntryConversionConverter x:Key="EntryConversionConverter"/>
        <conv:EntryEqualityVisibilityConverter x:Key="EntryEqualityVisibilityConverter"/>
        <conv:EntryTypeInfoConverter x:Key="EntryTypeInfoConverter"/>
        <conv:SearchOnlineConverter x:Key="SearchOnlineConverter"/>
        
        <CollectionViewSource x:Key="EntrySource">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="EntryType"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <Style TargetType="{x:Type dg:DataGridCell}" BasedOn="{StaticResource {x:Type dg:DataGridCell}}">
            <EventSetter Event="MouseDoubleClick" Handler="dgCell_MouseDoubleClick"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Transparent" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True"><!--Foreground="{StaticResource DefaultControlForegroundBrush}"-->
                            <Expander.Header>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Image Grid.Row="0" Grid.Column="0" Source="{Binding Name,Converter={StaticResource EntryTypeInfoConverter},ConverterParameter=icon}" Margin="0,0,5,0" VerticalAlignment="Center" Width="16" Height="16"  />
                                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name,Converter={StaticResource EntryTypeInfoConverter},ConverterParameter=friendlyName}" VerticalAlignment="Center"/>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name,Converter={StaticResource EntryTypeInfoConverter},ConverterParameter=path}" Foreground="#666666" Margin="0,1,0,0"
                                               TextTrimming="CharacterEllipsis" TextWrapping="Wrap"/>
                                    
                                </Grid>
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <dg:DataGrid x:Name="dgEntries" ItemsSource="{Binding Source={StaticResource EntrySource}}" PreviewKeyDown="dgEntries_PreviewKeyDown" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" 
                     CanUserAddRows="False" CanUserDeleteRows="False" VerticalAlignment="Stretch" SelectionMode="Extended" SelectionUnit="FullRow" CanUserSortColumns="True" 
                     RowHeaderWidth="25"  EnableRowVirtualization="True" GridLinesVisibility="Horizontal" AlternationCount="2">
        <dg:DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <dgp:DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </dg:DataGrid.GroupStyle>
        <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Binding="{Binding Name, Mode=OneWay}" Header="{lex:LocText LaunchMeNot.Core:Strings:Name}" IsReadOnly="True" Width="2*"/>
            <dg:DataGridTextColumn Binding="{Binding Path, Mode=OneWay}" Header="{lex:LocText LaunchMeNot.Core:Strings:Path}" IsReadOnly="True" Width="3*"/>
            <dg:DataGridTextColumn Binding="{Binding Arguments, Mode=OneWay}" Header="{lex:LocText LaunchMeNot.Core:Strings:Arguments}" IsReadOnly="True" MinWidth="60" Width="1*"/>
            <dg:DataGridTextColumn Binding="{Binding Description, Mode=OneWay}" Header="{lex:LocText LaunchMeNot.Core:Strings:Description}" IsReadOnly="True" Width="1*"/>
        </dg:DataGrid.Columns>
        <dg:DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:ConvertTo}" Icon="{StaticResource convert_16}">
                    <MenuItem Header="LaunchMeNot" Command="loc:LaunchMeNotCommands.Convert" 
                              CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryConversionConverter},ConverterParameter=LaunchMeNot}" Icon="{StaticResource launch_16}" 
                              Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryEqualityVisibilityConverter},ConverterParameter=LaunchMeNot}">
                    </MenuItem>
                    <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:AllUsersStartupFolder}" Command="loc:LaunchMeNotCommands.Convert" 
                              CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryConversionConverter},ConverterParameter=AllUsersStartup}" Icon="{StaticResource startup_16}" 
                              Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryEqualityVisibilityConverter},ConverterParameter=AllUsersStartup}">
                    </MenuItem>
                    <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:CurrentUserStartupFolder}" Command="loc:LaunchMeNotCommands.Convert" 
                              CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryConversionConverter},ConverterParameter=CurrentUserStartup}" Icon="{StaticResource startup_16}" 
                              Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryEqualityVisibilityConverter},ConverterParameter=CurrentUserStartup}">
                    </MenuItem>
                    <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:RegistryCurrentUserRun}" Command="loc:LaunchMeNotCommands.Convert" 
                              CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryConversionConverter},ConverterParameter=CurrentUserRun}" Icon="{StaticResource registry_16}" 
                              Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryEqualityVisibilityConverter},ConverterParameter=CurrentUserRun}">
                    </MenuItem>
                    <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:RegistryLocalMachineRun}" Command="loc:LaunchMeNotCommands.Convert" 
                              CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryConversionConverter},ConverterParameter=LocalMachineRun}" Icon="{StaticResource registry_16}" 
                              Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource EntryEqualityVisibilityConverter},ConverterParameter=LocalMachineRun}">
                    </MenuItem>
                </MenuItem>
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:CreateCopy}" Command="NavigationCommands.NextPage" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu}}" Icon="{StaticResource copy_16}" />
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:Edit}" Command="ApplicationCommands.Properties" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu}}" Icon="{StaticResource edit_16}" />
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:Remove}" Command="ApplicationCommands.Delete" CommandTarget="{Binding PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}" Icon="{StaticResource delete_16}" />
                <Separator/>
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:SearchOnline}" Icon="{StaticResource search_16}">
                    <MenuItem Header="Google" Command="ApplicationCommands.Find" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource SearchOnlineConverter},ConverterParameter='http://www.google.co.uk/search?q=%1'}"/>
                    <MenuItem Header="Bing" Command="ApplicationCommands.Find" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource SearchOnlineConverter},ConverterParameter='http://www.bing.com/search?q=%1'}" />
                    <MenuItem Header="ProcessLibrary.com" Command="ApplicationCommands.Find" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource SearchOnlineConverter},ConverterParameter='http://www.processlibrary.com/search/?q=%1'}" />
                </MenuItem>
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:ExplorerProperties}" Command="loc:LaunchMeNotCommands.ExplorerProperties" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                <MenuItem Header="{lex:LocText LaunchMeNot.Core:Strings:OpenFileLocation}" Command="ApplicationCommands.Open" CommandParameter="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu}}" Icon="{StaticResource directory_16}" />
            </ContextMenu>
        </dg:DataGrid.ContextMenu>
    </dg:DataGrid>
</UserControl>
